package org.papervision3d.core.proto
{
	import org.papervision3d.core.geom.Mesh3D;
	import org.papervision3d.core.geom.Face3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.core.geom.DrawTriangle;
	import org.papervision3d.objects.Cube;
	import org.papervision3d.core.geom.Vertex3D;
	import org.papervision3d.core.Matrix3D;
	import logout.pv.pathfinder.Map3D;
	import flash.display.BitmapData;
	import logout.pv.pathfinder.util.Raster;
	
	public class PathfinderCallback extends TraverseCallback
	{
		public function PathfinderCallback( rootNode : DisplayObject3D)
		{
			super();
			this.rootNode = rootNode;
		}
		
		override public function enter(node:DisplayObject3D):Boolean
        {
        //	trace( node.name );
        	curpath.push(node);
            return true;
        }
		override public function leave(node:DisplayObject3D):void
        {
        	curpath.pop();
        }
        
        override protected function inclip(triangle:DrawTriangle):Boolean
        {
            return true;
        }

        
		override public function face(face:Face3D, object:Mesh3D):void
        {
        	
        	//trace( object.name );
        	if( object.name != "wall" ) return;
        	
        	//trace( "PathfinderCallback find wall" );
        	//trace( face.v0,face.v1,face.v2);
        	
        	var v0 : Vertex3D = new Vertex3D( face.v0.x, face.v0.y, face.v0.z );
        	var v1 : Vertex3D = new Vertex3D( face.v1.x, face.v1.y, face.v1.z );
        	var v2 : Vertex3D = new Vertex3D( face.v2.x, face.v2.y, face.v2.z );
        	
        	var i : int = curpath.length;
        	while( --i >-1 ) {
        		localeToParentTransform(v0, curpath[i] );
        		localeToParentTransform(v1, curpath[i] );
        		localeToParentTransform(v2, curpath[i] );
        	}
        	
        	
        	segments.push(  { 	x0 :v0.x, z0 : v0.z,
        						x1 :v1.x, z1 : v1.z,
        						x2 :v2.x, z2 : v2.z
        					} ); 
        }
        
        private function localeToParentTransform( v : Vertex3D, parent : DisplayObject3D ) : void {
			var m:Matrix3D = parent.transform;
    		
    		//trace( "localeToParentTransform  : "+parent );
           
            var vx:Number = v.x;
            var vy:Number = v.y;
            var vz:Number = v.z;

            var sx:Number = vx * m.n11 + vy * m.n12 + vz * m.n13 + m.n14;
            var sy:Number = vx * m.n21 + vy * m.n22 + vz * m.n23 + m.n24;
            var sz:Number = vx * m.n31 + vy * m.n32 + vz * m.n33 + m.n34;

			v.x = sx;
			v.y = sy;
			v.z = sz;
    		//trace("  		", v.x, v.y, v.z );
		}
        private var rootNode : DisplayObject3D;
		protected var curpath:Array = new Array();
		
		public var segments : Array = new Array();
	}
}